// pages/course/offlineCourse/applyDetail/applyDetail.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderId: null,
        orderInfo: {},
        managerList: [],
        isManager: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            orderId: options.orderId
        })
        this.getOrderInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getManagerList: function() {
        const that = this
        let managerList = this.data.managerList
        let data = {
            online_id: this.data.orderInfo.online_id
        }
        common.post('Online/get_online_admin', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    managerList: managerList,
                })
            } else {
                managerList = managerList.concat(res)
                that.setData({
                    managerList: managerList
                })
                that.data.managerList.forEach((item) => {
                    if (item == wx.getStorageSync('userId')) {
                        that.setData({
                            isManager: true
                        })
                    }
                })
            }
        }).catch(function(e) {})
    },

    getOrderInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            online_buy_id: this.data.orderId,
            paramter: 1
        }
        common.post('Online/get_online_buy_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    orderInfo: {},
                })
            } else {
                that.setData({
                    orderInfo: res[0]
                })
                that.getManagerList()
            }
        }).catch(function(e) {})
    },

    confirmOrder: function(e) {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            online_buy_id: e.currentTarget.dataset.id,
            user_id: e.currentTarget.dataset.user
        }
        common.post('Online/end_online', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '订单确认成功',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.getOrderInfo()
                    }
                }
            })
        }).catch(function(e) {})
    }
})